import 'package:fish_redux/fish_redux.dart';
import 'package:kunlun_gas/AppViews/MainView/model/main_view_model.dart';
import 'package:kunlun_gas/PublicSource/utils/common_utils.dart';

import 'action.dart';
import 'state.dart';

Effect<EngineerIndoorReasonViewState> buildEffect() {
  return combineEffects(<Object, Effect<EngineerIndoorReasonViewState>>{
    // EngineerIndoorReasonViewAction.action: _onAction,
    EngineerIndoorReasonViewAction.onTapAddPic: _onTapAddPic,
    EngineerIndoorReasonViewAction.onTapPic: _onTapPic,
  });
}

void _onAction(Action action, Context<EngineerIndoorReasonViewState> ctx) {}

void _onTapAddPic(
    Action action, Context<EngineerIndoorReasonViewState> ctx) async {
  AssetEntityFile imgFile = await CommonUtils.showOrderImagePicker(ctx.context,
      type: 2, info: ctx.state.model);
  if (imgFile != null) {
    ctx.state.orderPhoto.add(imgFile);
    ctx.dispatch(EngineerIndoorReasonViewActionCreator.onAction());
  }
}

void _onTapPic(
    Action action, Context<EngineerIndoorReasonViewState> ctx) async {
  pushPhoto(file) async {
    await CommonUtils.showMediaAlbumView(
      ctx.context,
      initialIndex: ctx.state.orderPhoto.indexOf(file),
      files: ctx.state.orderPhoto,
    );
  }

  var file = action.payload;
  int res =
      await CommonUtils.showActionSheet(ctx.context, button: ['查看', '删除']);
  if (res == 1) {
    ctx.state.orderPhoto.remove(file);
    ctx.dispatch(EngineerIndoorReasonViewActionCreator.onAction());
  } else if (res == 0) {
    pushPhoto(file);
  }
}
